let cheerio = require('cheerio');
let superagent = require('superagent');
let charset = require('superagent-charset');
let eventproxy = require('eventproxy');
let async = require('async');

let ep = eventproxy();
charset(superagent);

let url = 'http://www.dytt8.net';
let newMovieLinks = [];

function main() {
    // .charset('gb2312') 解决编码问题
    superagent.get(url).charset('gb2312')
        .end(function (err, sres) {
            if (err) {
                console.log('抓取首页信息出错了');
                return next(err);
            }
            let $ = cheerio.load(sres.text);
            // 遍历 a 标签 
            $('.co_content2 ul a').each((idx, element) => {
                let $element = $(element);
                let eUrl = `http://www.dytt8.net${$element.attr('href')}`
                if (newMovieLinks.indexOf(eUrl) == -1) {
                    newMovieLinks.push(eUrl);
                }
            })
            getDetailMovie();
        })
}


function getDetailMovie() {
    // 控制并发
    async.mapLimit(newMovieLinks, 1, (itemUrl, callback) => {
        superagent.get(itemUrl).charset('gb2312')
            .end((err, sres) => {
                if (err) {
                    console.log("抓取详细页发生错误");
                    return next(err);
                }
                let $ = cheerio.load(sres.text);
                let downLink = $('#Zoom table a').text();
                let movieName = $('.title_all h1 font').text();
                console.log(downLink + "-----------" + movieName);

                callback(null, '请求成功');
            })
    }, (err, result) => {
        console.log('爬虫成功');
    })
}


main();